package com.lanou3g.mynewstest.module.topic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.beans.TopicDetailBeans;
import com.lanou3g.mynewstest.tools.CircleImageView;
import com.lanou3g.mynewstest.tools.MyRequestQueue;

/**
 * Created by dllo on 16/1/25.
 */
public class TopicDetailLatestAdapter extends BaseAdapter {
    private Context context;
    private TopicDetailBeans data;
    private int type = 0;

    public TopicDetailLatestAdapter(Context context, TopicDetailBeans data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        if (type == 0) {
            return null != data.getData().getLatestList() ? data.getData().getLatestList().size() : 0;
        } else {
            return null != data.getData().getHotList() ? data.getData().getHotList().size() : 0;
        }

    }

    @Override
    public Object getItem(int position) {
        if (type == 0) {
            return data.getData().getLatestList().get(position);
        } else {
            return data.getData().getHotList().get(position);
        }

    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TopicDetailViewHolder holder;
        if (type == 1) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_topic_detail, parent, false);
                holder = new TopicDetailViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (TopicDetailViewHolder) convertView.getTag();
            }
            holder.aName.setText(data.getData().getLatestList().get(position).getAnswer().getSpecialistName());
            holder.aContent.setText(data.getData().getLatestList().get(position).getAnswer().getContent());
            holder.qName.setText(data.getData().getLatestList().get(position).getQuestion().getUserName());
            holder.qContent.setText(data.getData().getLatestList().get(position).getQuestion().getContent());
            MyRequestQueue.setNetImage(holder.aImageView, data.getData().getLatestList().get(position).getAnswer().getSpecialistHeadPicUrl());
            MyRequestQueue.setNetImage(holder.qImageView, data.getData().getLatestList().get(position).getQuestion().getUserHeadPicUrl());
        } else {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_topic_detail, parent, false);
                holder = new TopicDetailViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (TopicDetailViewHolder) convertView.getTag();
            }
            holder.aName.setText(data.getData().getHotList().get(position).getAnswer().getSpecialistName());
            holder.aContent.setText(data.getData().getHotList().get(position).getAnswer().getContent());
            holder.qName.setText(data.getData().getHotList().get(position).getQuestion().getUserName());
            holder.qContent.setText(data.getData().getHotList().get(position).getQuestion().getContent());
            MyRequestQueue.setNetImage(holder.aImageView, data.getData().getHotList().get(position).getAnswer().getSpecialistHeadPicUrl());
            MyRequestQueue.setNetImage(holder.qImageView, data.getData().getHotList().get(position).getQuestion().getUserHeadPicUrl());
        }
        return convertView;
    }

    private class TopicDetailViewHolder {
        TextView qName, qContent, aName, aContent;
        CircleImageView qImageView, aImageView;

        public TopicDetailViewHolder(View view) {
            this.qName = (TextView) view.findViewById(R.id.item_topic_detail_q_name);
            this.qContent = (TextView) view.findViewById(R.id.item_topic_detail_q_content);
            this.aName = (TextView) view.findViewById(R.id.item_topic_detail_a_name);
            this.aContent = (TextView) view.findViewById(R.id.item_topic_detail_a_content);
            this.qImageView = (CircleImageView) view.findViewById(R.id.item_topic_detail_q_head);
            this.aImageView = (CircleImageView) view.findViewById(R.id.item_topic_detail_a_head);
        }
    }
    public void chageState(int type){
        this.type=type;
        notifyDataSetChanged();
    }
}
